<template>
  <div>
    <a-card :bordered="false" style="margin-bottom: 10px">
      <!-- 查询区域 -->
      <div class="table-page-search-wrapper" style="position: relative">
        <a-form-model :label-col="labelCol" :wrapper-col="wrapperCol" @keyup.enter.native="onQuery">
          <div class="mode_item">
            <a-form-model-item label="案事件名称">
              <a-input v-model="form.incidentName" placeholder="请输入" />
            </a-form-model-item>
            <a-form-model-item label="案事件编号">
              <a-input v-model="form.incidentCode" placeholder="请输入" />
            </a-form-model-item>
            <a-form-model-item label="状态">
              <j-search-select-tag placeholder="请选择" v-model="form.status" :dictOptions="statusOptions" />
            </a-form-model-item>
            <a-form-model-item label="所属单位">
              <j-select-depart
                v-model="form.departmentIds"
                :trigger-change="true"
                :multi="true"
                @rows="getSelectRows"
              />
            </a-form-model-item>
          </div>
          <div class="mode_item">
            <a-form-model-item label="任务名称">
              <a-input v-model="form.taskName" placeholder="请输入" />
            </a-form-model-item>
            <a-form-model-item label="任务编号">
              <a-input v-model="form.taskCode" placeholder="请输入" />
            </a-form-model-item>
            <a-form-model-item label="类型">
              <j-search-select-tag placeholder="请选择" v-model="form.type" :dictOptions="typeOptions" />
            </a-form-model-item>
          </div>
          <div class="mode_item mode_item_none">
            <a-form-model-item label="时间范围" class="zdy-form-item-label">
              <a-radio-group buttonStyle="solid" v-model="form.timeType">
                <a-radio-button v-for="(item, key) in options" :key="key" :value="item.value">
                  {{ item.text }}
                </a-radio-button>
              </a-radio-group>
              <a-range-picker
                v-if="form.timeType === '0'"
                :allowClear="true"
                v-model="rangePickerTime"
                valueFormat="YYYY-MM-DD"
                format="YYYY-MM-DD"
                @change="rangePickerChange"
                style="margin-left: 10px"
              />
            </a-form-model-item>
          </div>
          <div style="margin-top: 20px">
            <a-form-model-item :wrapper-col="{ offset: 0 }" style="margin-bottom: 0">
              <a-button class="Indormation-list-CX" type="primary" @click="onQuery"> 查询 </a-button>
              <a-button class="Indormation-list-CX" style="margin-left: 10px" @click="onResetForm"> 重置 </a-button>
            </a-form-model-item>
          </div>
        </a-form-model>
      </div>
    </a-card>
    <a-card :bordered="false">
      <!-- 表格展示 -->
      <div class="table-page-content">
        <custom-table
          ref="table"
          rowKey="id"
          :showAlert="false"
          :showSelection="false"
          :loading="loading"
          :columns="columns"
          :dataSource="pageData"
          @selectedRow="onSelectedRow"
          @change="onTableChange"
        >
          <!--状态-->
          <template slot="status" slot-scope="text, record">
            <span :class="[record.status == 2 ? 'green' : record.status == 3 ? 'red' : 'blue']">
              {{ getLabel(statusOptions, text) }}
            </span>
          </template>
          <!--操作-->
          <template slot="action" slot-scope="text, record">
            <a-button v-if="record.status == 1" @click="handleApprove(record)" type="link" style="padding:0;"
              >审批</a-button
            >
            <a-button
              v-else-if="record.status == 2 || record.status == 3"
              @click="handleDetails(record)"
              type="link"
              style="padding:0;"
              >查看</a-button
            >
          </template>
        </custom-table>
      </div>
      <!--弹窗区域 -->
      <!-- pageType，1：案事件管理 2：任务情报列表 3： 配置情报列表 4： 重点人列表 5：任务情报管理 6：种子号码列表 7：协查函列表、8 审批列表   10 负样本人员库 11 时空碰撞 -->
      <handle-modal ref="handleModalRef" isExamine @ok="onQuery" />
    </a-card>
  </div>
</template>

<script>
import moment from 'moment'
import * as countersignApi from '@/api/wxSmuggle/countersignServer.js'
import CustomTable from '@/components/zxd/Table/CustomTable'
import handleModal from './detailModal'

export default {
  components: {
    CustomTable,
    handleModal
  },
  data() {
    return {
      moment,
      labelCol: { span: 6 },
      wrapperCol: { span: 17 },
      rangePickerTime: [],
      show: false,
      closeTip: false,
      loading: false,
      visible: false,
      handleModalBool: false,
      isDisabled: true,
      reloading: false,
      selectKeys: [],
      form: {},
      statusOptions: [
        { text: '全部', value: '' },
        // { text: '未审批', value: '0', },
        { text: '待审批', value: '1' },
        { text: '通过', value: '2' },
        { text: '驳回	', value: '3' }
      ],
      typeOptions: [
        { text: '全部', value: '' },
        { text: '案事件', value: '1' },
        { text: '情报事件', value: '2' }
      ],
      options: [
        { value: '2', text: '今天' },
        { value: '1', text: '昨天' },
        { value: '3', text: '近三天' },
        { value: '4', text: '近七天' },
        { value: '0', text: '自定义' }
      ],
      columns: [
        {
          title: '序号',
          dataIndex: 'order',
          width: 60,
          align: 'center',
          customRender: (text, record, index) =>
            `${(this.pageFilter.pageNo - 1) * this.pageFilter.pageSize + (index + 1)}` //序号分页后连续
        },
        {
          title: '所属单位',
          dataIndex: 'departName',
          width: 100,
          align: 'left',
          customRender: text => {
            return <j-ellipsis value={text} length={16} />
          }
        },
        {
          title: '任务编号',
          dataIndex: 'taskCode',
          width: 100,
          align: 'left',
          customRender: text => {
            return <j-ellipsis value={text} length={26} />
          }
        },
        {
          title: '任务名称',
          dataIndex: 'taskName',
          width: 120,
          align: 'left',
          customRender: text => {
            return <j-ellipsis value={text} length={16} />
          }
        },
        {
          title: '案事件编号',
          dataIndex: 'incidentCode',
          width: 100,
          align: 'left',
          customRender: text => {
            return <j-ellipsis value={text} length={26} />
          }
        },
        {
          title: '案事件名称',
          dataIndex: 'incidentName',
          width: 150,
          align: 'left',
          customRender: text => {
            return <j-ellipsis value={text} length={16} />
          }
        },
        {
          title: '状态',
          dataIndex: 'status',
          width: 80,
          align: 'center',
          scopedSlots: { customRender: 'status' }
        },
        {
          title: '任务起始时间',
          dataIndex: 'eventTime',
          width: 140,
          align: 'center'
          // filters: [
          //   { text: '创建时间排序', value: '1' },
          //   { text: '结束时间排序', value: '2' },
          //   { text: '临期任务', value: '3' },
          // ],
        },
        {
          title: '操作',
          width: 100,
          align: 'center',
          dataIndex: 'action',
          // slots: { title: 'customIsAction' },
          scopedSlots: { customRender: 'action' }
        }
      ],
      pageFilter: {
        pageNo: 1,
        pageSize: 10
      },
      pageData: {}
    }
  },
  created() {},
  async mounted() {
    this.onQuery()
  },
  methods: {
    onQuery() {
      this.pageFilter.pageNo = 1
      this.refreshTable()
      this.$store.dispatch('OperateAjax', true)
    },
    onResetForm() {
      this.form = {}
      this.pageFilter.pageNo = 1
      this.pageFilter.pageSize = 10
      this.refreshTable()
    },
    onSelectedRow(keys) {
      this.selectKeys = keys
      this.isDisabled = keys.length > 0 ? false : true
    },
    onTableChange(pagination, pageFilter, sorter) {
      let keyMap = { currentPage: 'pageNo' }
      let paginations = Object.keys(pagination).reduce((newData, key) => {
        let newKey = keyMap[key] || key
        newData[newKey] = pagination[key]
        return newData
      }, {})
      this.pageFilter = Object.assign({}, this.pageFilter, paginations)
      this.refreshTable()
    },
    refreshTable() {
      let form = Object.assign({}, this.form, this.pageFilter)
      this.loading = true
      countersignApi
        .getProcessInfoList(form)
        .then(res => {
          if (res.success) {
            this.pageData = res.result
            this.loading = false
            this.$refs.table.onClearRowKeys()
          } else {
            this.loading = false
          }
        })
        .catch(e => {
          this.loading = false
          this.$message.warn('请求失败！')
        })
    },
    // 弹窗成功接收
    modalFormOk(type) {
      // 导出不刷新列表
      if (type == 2) return
      // 成功时，重载列表
      this.refreshTable()
      // 清空列表选中
      this.$refs.table.onClearRowKeys()
    },
    handleApprove(record) {
      this.$refs.handleModalRef.show(record)
    },
    // 详情
    handleDetails(record) {
      this.$refs.handleModalRef.show(record)
    },
    // 生效范围
    getSelectRows(rows) {
      if (rows && rows.length > 0) {
        let rangeNames = rows.map(v => v.title).join(',')
        this.$set(this.form, 'departmentNames', rangeNames)
      } else {
        this.$set(this.form, 'departmentNames', '')
      }
    },
    // 时间范围
    rangePickerChange(date) {
      this.$set(this.form, 'startTime', date[0])
      this.$set(this.form, 'endTime', date[1])
    },
    /** 重新加载组件 */
    reload() {
      this.reloading = true
      this.$nextTick(() => (this.reloading = false))
    },
    getLabel(datas, value) {
      let text = ''
      let item = datas.find(obj => {
        return obj.value == value
      })
      console.log(item)
      if (item && Object.keys(item).length > 0) {
        text = item.text
      }
      return text
    }
  }
}
</script>
<style lang="less" scoped>
.mode_item {
  display: flex;
  border-bottom: 1px #dadada dashed;
  > div {
    flex: 0 0 25%;
  }
  & > * {
    width: 100%;
    margin-bottom: 0;
    padding: 5px 0;
  }
  &.mode_item_none {
    > div {
      flex: inherit;
    }
  }
  .zdy-form-item-label {
    /deep/.ant-form-item-label {
      width: 82px;
    }
    &.xzqh-form-item-label {
      /deep/.ant-form-item-control-wrapper {
        .ant-form-item-children {
          display: -webkit-box;
        }
      }
      /deep/.area-select {
        // margin-top: 4px;
        line-height: 1;
      }
      /deep/.select-tag {
        width: 162px !important;
        margin-left: 10px;
        margin-top: 3px;
      }
      // /deep/.area-select .area-selected-trigger {
      //   line-height: 1.15;
      // }
      // /deep/.ant-select-lg .ant-select-selection--single{
      //   height: 34px;
      // }
      // /deep/.ant-select-lg .ant-select-selection__rendered {
      //   line-height: 34px;
      //   font-size: 14px;
      // }
    }
  }
}
.search-wrapper-btn {
  position: absolute;
  right: 0;
  top: 15px;
  z-index: 1;
}
/deep/.table-operator {
  display: flex;
  justify-content: space-between;
}
/deep/.table-custom-cols {
  top: -48px !important;
}
.custom-ewm {
  padding: 14px;
  background-color: #fff;
  border: 1px #ccc solid;
  border-radius: 6px;
  display: flex;
  flex-direction: column;
}
/deep/.ant-dropdown-menu-item {
  text-align: center;
  border-bottom: 1px #f5f5f5 solid;
  &:last-child {
    border-bottom: 0;
  }
}
/deep/.anticon.anticon-down {
  font-size: 14px !important;
}

.table-page-content {
  /deep/.ant-table-thead > tr > th,
  /deep/.ant-table-tbody > tr > td {
    padding-left: 5px;
    padding-right: 5px;
  }
  /deep/.ant-table-thead > tr:first-child > th:first-child,
  /deep/.ant-table-tbody > tr > td:first-child {
    padding-left: 10px;
  }
  .green {
    color: #52c41a;
  }
  .red {
    color: #ff4d4f;
  }
  .blue {
    color: #1890ff;
  }
}
</style>
